'''
Created on 09.08.2011

@author: osman
'''

import threading
from opf.exceptions import OPFError

class NoOPFContextForThisThread(OPFError): pass

_p = threading.local()

def setContext(ont):
    global _p
    _p.context = ont

class _DontUseThis: pass

def getContext():
    global _p
    return getattr(_p, 'context', None)

def context(default = _DontUseThis):
    global _p
    context = getattr(_p, 'context', None)
    if context is None: raise NoOPFContextForThisThread() 
    return context

def addInstance(inst):
    return context()._addInstance(inst)

def addArc(arc):
    return context()._addArc(arc)

def addPredicate(p):
    return context()._addPredicate(p)

def noChecks():
    return context()._prohibitConsistencyChecks()

class Context(threading.local):
    def _addInstance(self, inst): pass
    def _addArc(self, arc): pass
    def _addPredicate(self, p): pass
    def _prohibitConsistencyChecks(self): return False
